﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SyCODE.Component.Security;
using System.Xml;
using SyCODE.Component.DataFactory;
using System.Collections.Specialized;
using System.Data.Common;

namespace SyCODE.Blog
{
    /// <summary>
    /// CheckLogin 的摘要说明
    /// </summary>
    public class CheckLogin : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/xml";
            context.Response.ExpiresAbsolute = DateTime.Now.AddYears(-50);

            XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, context.Response.ContentEncoding);
            writer.WriteStartDocument();

            writer.WriteStartElement("message");
			Ticket ticket = Ticket.Get(FormsAuthentication.FormsCookieName);
            if (Ticket.Authenticate(ticket))
            {
                writer.WriteAttributeString("account", ticket.Name.ToLower());
                writer.WriteAttributeString("name", HttpUtility.HtmlDecode(ticket.UserData));

                NameValueCollection nvc = new NameValueCollection();
                nvc.Add("u", ticket.Name);
                DBAM dbam = new DBAM();
                dbam.BuildParameters("checkmessage", nvc);
                try
                {
                    DbDataReader dr = dbam.ExecuteReader();
                    if (dr.Read())
                    {
                        writer.WriteAttributeString("private", dr.GetValue(0).ToString());
                        writer.WriteAttributeString("system", dr.GetValue(1).ToString());
                    }
                }
                finally
                {
                    dbam.Dispose();
                }
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
        }
    }
}